﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;
namespace LogicUniversity
{
    public partial class Login : System.Web.UI.Page
    {
        LoginController loginController = new LoginController();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string userID = txtUserID.Text.Trim();
            string password = txtPassword.Text.Trim();
            if (userID != "" && password != "")
            {
                var employee = loginController.getEmployeeID(userID);
                if (employee != null)
                {
                    if (employee.Employee_ID.Equals(userID) & employee.Password.Equals(password))
                    {
                        Session["UserID"] = employee.Employee_ID;
                        Session["Role"] = employee.Role;
                        Session["TempRole"] = employee.Temp_Role;
                        
                        pageRedirectByRole(employee.Role.Trim());

                    }
                    else
                    {
                        lblError.Text = "EmployeeID and password doesn't match";
                    }
                }
                else
                {
                    lblError.Text = "Employee ID and password cannot be blank.";
                }
            }
            else
            {
                lblError.Text="Employee ID and password cannot be blank.";
            }
        }
        protected void pageRedirectByRole(string role)
        {
            if (role.Equals("Department Head"))
            {
                Response.Redirect("~/Department_Head/Approve.aspx");
            }
            else if (role.Equals("Manager"))
            {
                Response.Redirect("~/Manager/ApproveAdjustment.aspx");
            }
            else if (role.Equals("Employee"))
            {
                Response.Redirect("~/Employee/MakeRequest.aspx");
            }
            else if (role.Equals("Representative"))
            {
                Response.Redirect("~/Employee/MakeRequest.aspx");
            }
            else if (role.Equals("Supervisor"))
            {
                Response.Redirect("~/Supervisors/ApproveAdjustment.aspx");
            }
            else if (role.Equals("Clerk"))
            {
                Response.Redirect("~/clerks/RequisitionList.aspx");
            }
        }
    }
}